package com.yc.weixin.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.yc.utils.YcUtils;
import com.yc.weixin.bean.responseMessage.ResponseArticleMessage;
import com.yc.weixin.bean.responseMessage.ResponseImageMessage;
import com.yc.weixin.bean.responseMessage.ResponseMusicMessage;
import com.yc.weixin.bean.responseMessage.ResponseTextMessage;
import com.yc.weixin.bean.responseMessage.ResponseVideoMessage;
import com.yc.weixin.bean.responseMessage.ResponseVoiceMessage;

public class MessageUtil {
	public static final String REQUEST_MESSAGE_TYPE_TEXT="text";
	public static final String REQUEST_MESSAGE_TYPE_IMAGE="image";
	public static final String REQUEST_MESSAGE_TYPE_VOICE="voice";
	public static final String REQUEST_MESSAGE_TYPE_VIDEO="video";
	public static final String REQUEST_MESSAGE_TYPE_LOCATION="location";
	public static final String REQUEST_MESSAGE_TYPE_LINK="link";
	public static final String REQUEST_MESSAGE_TYPE_EVENT="event";
	
	
	//订阅
	public static final String EVENT_TYPE_SUBSCRIBE="subscribe";
	public static final String EVENT_TYPE_UNSUBSCRIBE="unsubscribe";
	public static final String EVENT_TYPE_SCAN="scan";
	public static final String EVENT_TYPE_LOCATION="location";
	public static final String EVENT_TYPE_CLICK="CLICK";
	
	public static final String RESPONSE_MESSAGE_TYPE_TEXT="text";
	public static final String RESPONSE_MESSAGE_TYPE_IMAGE="image";
	public static final String RESPONSE_MESSAGE_TYPE_VOICE="voice";
	public static final String RESPONSE_MESSAGE_TYPE_VIDEO="video";
	public static final String RESPONSE_MESSAGE_TYPE_MUSIC="music";
	public static final String RESPONSE_MESSAGE_TYPE_NEWS="news";
	
	/**
	 * 解析微信服务器发过来的请求
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws DocumentException 
	 */
	public static Map<String,String> parseXml( HttpServletRequest request) throws IOException, DocumentException{
		Map<String,String> map=new HashMap<String,String>();
		InputStream inputStream=null;
		try {
			inputStream = request.getInputStream();
			SAXReader reader=new SAXReader();
			Document document=reader.read(inputStream);
			Element root=document.getRootElement();
			List<Element> elementList=root.elements();
			for( Element e: elementList){
				map.put(    e.getName(), e.getText());
			}
		} catch (IOException e) {
			YcUtils.logger.error(e.getMessage());
			throw e;
		} catch (DocumentException e) {
			YcUtils.logger.error(e.getMessage());
			throw e;
		}finally{
			if(   inputStream!=null){
				try {
					inputStream.close();
				} catch (IOException e) {
					YcUtils.logger.error(e.getMessage());
				}
				inputStream=null;
			}
		}
		return map;
	}
	
	/**
	 * 把回送给用户的信息转换成xml
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws DocumentException 
	 */
	private static String parseXmlStringFromObject( Object o) throws Exception{
		ByteArrayOutputStream baos=null;
		try {
			JAXBContext context = JAXBContext.newInstance(o.getClass());
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			
			 baos=new ByteArrayOutputStream();
			marshaller.marshal(o, baos);
			
			return baos.toString();
		} catch (Exception e) {
			YcUtils.logger.error(  e.getMessage() );
			throw e;
		} finally{
			if( baos!=null){
				try {
					baos.close();
				} catch (IOException e) {
					YcUtils.logger.error(  e.getMessage() );
					throw e;
				}
			}
			baos=null;
		}
	}
	
	
	/**
	 * 将文本消息转换成xml
	 * @param textMessage
	 * @return
	 * @throws Exception 
	 */
	public static String messageToXml( ResponseTextMessage textMessage) throws Exception{
		return parseXmlStringFromObject(textMessage);
	}
	
	
	/**
	 * 将图片消息转换成xml
	 * @param imageMessage
	 * @return
	 * @throws Exception 
	 */
	public static String messageToXml( ResponseImageMessage imageMessage) throws Exception{
		return parseXmlStringFromObject(imageMessage);
	}
	
	/**
	 * 将语音消息转换成xml
	 * @param voiceMessage
	 * @return
	 * @throws Exception 
	 */
	public static String messageToXml( ResponseVoiceMessage voiceMessage) throws Exception{
		return parseXmlStringFromObject(voiceMessage);
	}
	
	/**
	 * 将视频消息转换成xml
	 * @param videoMessage
	 * @return
	 * @throws Exception 
	 */
	public static String messageToXml( ResponseVideoMessage videoMessage) throws Exception{
		return parseXmlStringFromObject(videoMessage);
	}
	
	/**
	 * 将音乐消息转换成xml
	 * @param musicMessage
	 * @return
	 * @throws Exception 
	 */
	public static String messageToXml( ResponseMusicMessage musicMessage) throws Exception{
		return parseXmlStringFromObject(musicMessage);
	}
	
	/**
	 * 将图文消息转换成xml
	 * @param articleMessage
	 * @return
	 * @throws Exception 
	 */
	public static String messageToXml( ResponseArticleMessage articleMessage) throws Exception{
		return parseXmlStringFromObject(articleMessage);
	}
	
	
	/** 
	 * emoji表情转换(hex -> utf-16) 
	 * @param hexEmoji 
	 * @return 
	 */  
	public static String emoji(int hexEmoji) {  
	    return String.valueOf(Character.toChars(hexEmoji));  
	} 
	
}










